package com.financing.model;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import java.util.List;
import java.util.Map;
import com.jfinal.plugin.activerecord.IBean;

@SuppressWarnings({"serial", "unchecked"})
public abstract class BaseBean<M extends BaseBean<M>> extends Model<M> implements IBean {

	public M setId(java.lang.Integer id) {
		set("id", id);
		return (M)this;
	}

	public java.lang.Integer getId() {
		return get("id");
	}	
	
	public List<M> findAll(String table) { 
		return find("select * from "+table);
	}
	
	public static String params(String table, String...params) {
		String where  = "";	
		for(int i=0; i<params.length; i++){
			if(i>=params.length){
				where += " AND "+params[i]+" = '"+params[i+1]+"' ";
			}
		}
		return "SELECT * FROM "+table+" WHERE 1=1 "+where;
	}

	public M findFirstByParams(String table, Map<Object,Object> params) {
		return findFirstByParams(table,params,"create_time desc");
	} 
	
	public M findFirstByParams(String table, Map<Object, Object>map, String orderBy) {
		String where  = "";	
		for(Object key : map.keySet()){
			where += " AND "+(String) key+" = '"+(String) map.get(key)+"' ";
		}
		return findFirst("SELECT * FROM "+table+" WHERE 1=1 "+where+" order by "+orderBy);
	}
	
	public Page<M> paginate(String table, int pageNumber, int pageSize) {
		return paginate(pageNumber, pageSize, "select *", "from "+table);
	}
}
